/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufjf.posjava.cm.util;

import java.sql.*;

/**
 *
 * @author Filipe
 */
public class ConnectionFactory {
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            return DriverManager.getConnection("jdbc:mysql://localhost/BDclinica", "root", "");

        } catch (SQLException ex) {
            System.out.println("Erro de conexão: Banco não encontrado!!!");
            return null;
        } catch (ClassNotFoundException ex) {
            System.out.println("Erro de Conexão: Driver não encontrado!!!");
            return null;
        }
    }

    public static void main(String[] args) {
        try {
            Connection c = getConnection();

            System.out.println(" BD: " + c.getCatalog());

            PreparedStatement pstm = c.prepareStatement("select * from profissional"
                    + " where nome like ?");
            pstm.setString(1, "%a%");
            ResultSet rs = pstm.executeQuery();
            while (rs.next()) {
                System.out.println("--> (" + rs.getInt("id") + ")" + rs.getString("nome"));
            }
            pstm.setString(1, "%esp%");
            rs = pstm.executeQuery();
            while (rs.next()) {
                System.out.println("--> (" + rs.getInt("id") + ")" + rs.getString("nome"));
            }



//            Statement st = c.createStatement();
//            
//            st.executeUpdate("INSERT INTO  SuperCategoria(descricao) "
//                    + "VALUES ('Esporte')");
//            
//            ResultSet rs = st.executeQuery("select * from SuperCategoria");
//            
//            while (rs.next()) {                
//                System.out.println("--> ("+rs.getInt("id") +")"+ rs.getString("descricao"));
//            }
//            
//            ResultSet rs1 = st.executeQuery("select count(*) from SuperCategoria");
//            rs1.next();
//            System.out.println("-->"+rs1.getInt(1));
//            
            c.close();

        } catch (ClassNotFoundException ex) {
            System.out.println("Erro: " + ex.getMessage());
        } catch (SQLException ex) {
            System.out.println("Erro: " + ex.getMessage());
        }
    }    
}
